<?php
include 'includes/application_top.inc';
include 'includes/paginate.php';
include 'auth.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="Easy Living - Responsive Real Estate Template">
<meta name="keywords" content="Themes, real estate, responsive, themeforest, Templates">
<meta name="author" content="Rype Pixel [Chris Gipple]">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome Listing grid Reports</title>
<!-- html5 support in IE8 and later -->
<script src="../../html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<!-- CSS file links -->
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
<link href="css/styles.css" rel="stylesheet" type="text/css" media="all" />
<link href="css/responsive.css" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<!-- Start Header -->
<?php include 'includes/header.php';?>
<!-- End Header -->
<!-- start subheader -->
<p>&nbsp;</p>
<!-- start recent properties -->
<section class="properties">
    <div class="container">
    	<?php include 'includes/left.php';?>    
        <div class="row">
            <div class="col-sm-6">
                <div class="propertyItem">
                		<?php  
                		//echo "<pre>";
                		//print_r($_REQUEST);                		 
								 $datepickerstart = @$_REQUEST['datepickerstart'];
								 $datepickerend = @$_REQUEST['datepickerend'];
								 $search = @$_REQUEST['search'];
						 $curRequest = "&datepickerstart=".$datepickerstart."&datepickerend=".$datepickerend."&search=".$search;
                		        $user_id = $_SESSION['u_id'];
								$RefAuthid = 5;
								$authCheck  = authentication($RefAuthid,$user_id); 
								if($authCheck==1)
								{?>
                	 <?php 	$search1 = trim(@$_REQUEST['os'])."&nbsp;".trim(@$_REQUEST['handset'])."&nbsp;".trim(@$_REQUEST['shorturl_code'])."&nbsp;".trim(@$_REQUEST['long_url'])."&nbsp;".trim(@$_REQUEST['datepickerstart'])."&nbsp;".trim(@$_REQUEST['datepickerend']);?>
                	 	<form name="frm1" id="frm1" action="listing_grid_piechat_report.php" method="GET" onsubmit="return checkForm(this);">
                	  	<table border="1" class="propertyDetails">
                	  		<tr>
                	  		<td>
                	  		<table border="1" class="propertyDetails">
                	  		<tr><td>From Date:</td><td><input type="text" name="datepickerstart" id="datepickerstart" value="<?php echo @$_REQUEST['datepickerstart'];?>" /><td>To Date:</td><td><input type="text" name="datepickerend" id="datepickerend" value="<?php echo @$_REQUEST['datepickerend'];?>" /></td></tr>
                	  		<tr><td>Short Url:</td><td><input type="text" name="shorturl_code" id="shorturl_code" value="<?php echo @$_REQUEST['shorturl_code'];?>" /></td><td>Long Url: </td><td><input type="text" name="long_url" id="long_url" value="<?php echo @$_REQUEST['long_url'];?>" /></td></tr>
                	  		<tr><td>Hand set:</td><td><input type="text" name="handset" id="handset" value="<?php echo @$_REQUEST['handset'];?>" /></td><td>OS: </td><td><input type="text" name="os" id="os" value="<?php echo @$_REQUEST['os'];?>" /></td></tr>
                	  		<tr><td colspan="3">&nbsp;
                                                <input type="checkbox" name="hourwise" id="hourwise" value="HOURWISE" <?php if(@$_REQUEST['hourwise']=="HOURWISE"){?>checked="checked"<?php }?> />&nbsp;Hour wise
                                                &nbsp;&nbsp;<input type="checkbox" name="datewise" id="datewise" value="DATEWISE"  <?php if(@$_REQUEST['datewise']=="DATEWISE"){?>checked="checked"<?php }?> />&nbsp;Date wiseacre
                                                &nbsp;&nbsp;<input type="checkbox" name="piechart" id="piechart" value="PIECHART"  <?php if(@$_REQUEST['piechart']=="PIECHART"){?>checked="checked"<?php }?> />&nbsp;Pie Chart</td>
                                            <td><input type="submit" name="submit" value="Submit" /><a href="listing_grid_piechat_report.php">Clear All</a></td></tr>
                	  		 <?php 
					 if($search!=''){
					 echo '<tr><td colspan="4">Search by: '.$search1.'</td></tr>'; }?> 
                	  	</table></td>
                	  		<td><table border="1" class="propertyDetails">
                	  		<tr><td>Display Fields</td></tr>
                	  		<tr><td> <input type="checkbox" name="msisdndisplay" id="msisdndisplay" value="msisdndisplay" <?php if(@$_REQUEST['msisdndisplay']=="msisdndisplay"){?>checked="checked"<?php }?> /> MSISDN</td></tr>
                	  		<tr><td><input type="checkbox" name="useragentdisplay" id="useragentdisplay" value="useragentdisplay" <?php if(@$_REQUEST['useragentdisplay']=="useragentdisplay"){?>checked="checked"<?php }?> /> User Agent</td></tr>
                	  		<tr><td><input type="checkbox" name="osdisplay" id="osdisplay" value="osdisplay" <?php if(@$_REQUEST['osdisplay']=="osdisplay"){?>checked="checked"<?php }?> /> Os</td></tr>
                	  		<tr><td><input type="checkbox" name="shorturldisplay" id="shorturldisplay" value="shorturldisplay" <?php if(@$_REQUEST['shorturldisplay']=="shorturldisplay"){?>checked="checked"<?php }?>/> Short Url</td></tr>
                	  		<tr><td><input type="checkbox" name="longurldisplay" id="longurldisplay" value="longurldisplay" <?php if(@$_REQUEST['longurldisplay']=="longurldisplay"){?>checked="checked"<?php }?>/> Long Url</td></tr>
                	  	</table></td>
                	  		</tr>
                	  		 
                	  	</table>
                	  </form>
                
                    <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div><hr />
                	 <?php //print_r($_REQUEST);echo "<hr>";?>
					<table border="1" class="propertyDetails">
                         <tr style="background-color:#e7e7e7;">
                         	<?php if(@$_REQUEST['msisdndisplay']=="msisdndisplay"){?>
                       	    <td>MSISDN</td><?php } if(@$_REQUEST['useragentdisplay']=="useragentdisplay"){?>
                            <td>User Agent</td>
                            <?php }  if(@$_REQUEST['osdisplay']=="osdisplay"){?>
                            <td>OS</td>
                            <?php } if(@$_REQUEST['shorturldisplay']=="shorturldisplay"){?>
                            <td>Short Url</td><?php }
							if(@$_REQUEST['longurldisplay']=="longurldisplay"){?>
                            <td>Long Url</td>
                            <?php }?>
                            <?php
                            if(@$_REQUEST['datewise']=="DATEWISE")
							{
							?>
                            <td>Date</td>
                            <?php	
							}
                            ?>
                            <?php
                            if(@$_REQUEST['hourwise']=="HOURWISE")
							{
							?>
                            <td>Hour</td>
                            <?php
							}
                            ?>
                             
                              
                            <td>Hit Count </td>
                         </tr>
                    	<?php   
                    	$per_page = 25;  
						$datepickerstart = date("Y-m-d 00:00:00",strtotime(@$_REQUEST['datepickerstart']));
						$datepickerend = date("Y-m-d 24:59:59",strtotime(@$_REQUEST['datepickerend']));	
                                                
//						    $select = "SELECT * FROM track_url  WHERE ";
						   $select =  "SELECT t_id,msisdn,useragent,os,shorturl_code,long_url, count(long_url) as urlcount,date(datetime) as datetimeprocess, HOUR(datetime) AS Hour, COUNT(*) AS `user_count` FROM track_url WHERE 1 ";
							if(@trim($_REQUEST['datepickerstart'])!="" || @trim($_REQUEST['datepickerstart'])!=""){
								  $select .=" AND datetime >= '$datepickerstart' AND datetime <= '$datepickerend' ";	
							  }
                                                
							  
						if(@trim($_REQUEST['os']) != "")
                                                {
                                                    $os = trim($_REQUEST['os']);
                                                    $select .=" AND os = '$os'";
                                                } 
                                                
                                                if(trim(@$_REQUEST['handset'])!="")
                                                {
                                                    $handset = trim($_REQUEST['handset']);
//                                                    $select .=" AND os = '$handset'";
                                                }
                                                
                                                if(@trim($_REQUEST['shorturl_code']) != "")
                                                {
                                                    $shortCode = trim($_REQUEST['shorturl_code']);
                                                    $shortCode = explode('/', $shortCode);
                                                    $cnt = count($shortCode);
                                                    $code = $shortCode[$cnt - 1];
                                                    $select .=" AND shorturl_code = '$code'";
                                                }
                                                
                                                if(@trim($_REQUEST['long_url']) != "")
                                                {
                                                    $longUrl = trim($_REQUEST['long_url']);
                                                    $select .= " AND long_url LIKE '%$longUrl%' ";
                                                }
                                                
                                                if(trim(@$_REQUEST['handset'])!="")
                                                {
                                                    $search = trim(@$_REQUEST['handset']);
//                                                    $select .= " AND handset LIKE '%$search%'";
                                                }
                                                if(trim(@$_REQUEST['os'])!="")
                                                {
                                                    $search = trim(@$_REQUEST['os']);
                                                    $select .= " AND os LIKE '%$search%' ";
                                                }


//print_r($_REQUEST);
//useragent,os,shorturl_code,long_url
$arrselect = array();


					if(isset($_REQUEST['msisdndisplay'])){$arrselect[] = $_REQUEST['msisdndisplay'];}
					if(isset($_REQUEST['useragentdisplay'])){$arrselect[] = $_REQUEST['useragentdisplay'];}
					if(isset($_REQUEST['osdisplay'])){$arrselect[] = $_REQUEST['osdisplay'];}
					if(isset($_REQUEST['shorturldisplay'])){$arrselect[] = $_REQUEST['shorturldisplay'];}
					if(isset($_REQUEST['longurldisplay'])){$arrselect[] = $_REQUEST['longurldisplay'];}
 
                                $selCount= count($arrselect);
								$s = 0;
								$groupby = "";
								for($w=0;$w<$selCount;$w++)
								{
								     //$groupby .=$arrselect[$w];
								  
								    if($arrselect[$w]=="msisdndisplay")
										{
										$groupby .=' msisdn';	
										}
								   if($arrselect[$w]=="useragentdisplay")
										{
										$groupby .=' useragent';	
										}
 									if($arrselect[$w]=="osdisplay")
										{
										$groupby .=' os';	
										}
									 if($arrselect[$w]=="shorturldisplay")
										{
										$groupby .='shorturl_code';	
										}
 									if($arrselect[$w]=="longurldisplay")
										{
										$groupby .='long_url';	
										}
									if($w==$s&& $w!=$selCount-1)
									 {
									   $groupby .= ", ";	
								      }
								$s++;}

//								echo $groupby;
								/*if(@$_REQUEST['msisdndisplay']=="msisdndisplay"){
									$groupby .=" msisdn ";
								}
								if(@$_REQUEST['useragentdisplay']=="useragentdisplay" && $_REQUEST['msisdndisplay']=="msisdndisplay"){
									$groupby .=" msisdn, useragent";
								}*/
					    $select .= "  AND date(NOW()) OR datetime IS NULL  GROUP by ".$groupby."  ORDER BY  t_id DESC";
					   
//					    echo $select;
					   
					   

						$result = mysql_query($select);
						$total_results = @mysql_num_rows($result);
						$pagesUrl = "tpages=".@$_REQUEST['tpages']."&page=".@$_REQUEST['page'];
						$total_pages = ceil($total_results / $per_page);//total pages we going to have
						//-------------if page is setcheck------------------//
                                                $show_page = 1;
						if (isset($_GET['page'])) {
						    $show_page = $_GET['page'];             //it will telles the current page
						    if ($show_page > 0 && $show_page <= $total_pages) {
						        $start = ($show_page - 1) * $per_page;
						        $end = $start + $per_page;
						    } else {
						        // error - show first set of results
						        $start = 0;              
						        $end = $per_page;
						    }
						} else {
						    // if page isn't set, show first set of results
						    $start = 0;
						    $end = $per_page;
						}
						// display pagination
						$page = intval(@$_GET['page']);
						
						$tpages=$total_pages;
						if ($page <= 0)
						    $page = 1;
                    	
						 $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
						 
                    echo '<div class="pagination"><ul>';
                    if ($total_pages > 1) {
                       // echo paginate($reload, @$show_page, $total_pages);
						 
					echo 	paginationprocess($reload, @$show_page, $total_pages,$curRequest);
					 
                    }
                    echo "</ul></div>";
					
					if($total_results>0)
					{
					
						for ($i = $start; $i < $end; $i++) {
	                        // make sure that PHP doesn't try to show results that don't exist
	                        if ($i == $total_results) {
	                            break;
	                        }
							
	                       $id= mysql_result($result, $i, 't_id');
						   $long_url  =mysql_result($result, $i, 'long_url');
						   $useragent  =mysql_result($result, $i, 'useragent');
						   $shorturl_code = ROOTURL. mysql_result($result, $i, 'shorturl_code');
						   $long_url  =mysql_result($result, $i, 'long_url');
						   $msisdn  =mysql_result($result, $i, 'msisdn');
						   $os  =mysql_result($result, $i, 'os');
						   $urlcount  =mysql_result($result, $i, 'urlcount');

						   $datetimeprocess  =mysql_result($result, $i, 'datetimeprocess');
						   $Hour  =mysql_result($result, $i, 'Hour');
						    
	                        // echo out the contents of each row into a table
	                        
	                        echo '<tr class="propertyDetails1">';
							if(@$_REQUEST['msisdndisplay']=="msisdndisplay"){
	                        echo '<td>' .$msisdn . '</td>';
							}
							if(@$_REQUEST['useragentdisplay']=="useragentdisplay")
							{
							echo '<td>'.$useragent.'</td>';
							}
							if(@$_REQUEST['osdisplay']=="osdisplay")
							{
							echo '<td>' .$os . '</td>';
							}
							// echo '<td>' . mysql_result($result, $i, 'datetime') . '</td>';
							if(@$_REQUEST['shorturldisplay']=="shorturldisplay")
							{
	                        echo '<td><a target="_blank" href="'.$shorturl_code .'">' .$shorturl_code . '</a></td>';
							}
							if(@$_REQUEST['longurldisplay']=="longurldisplay")
							{
	                         echo '<td>' . $long_url. '</td>';   
							}
							
                            if(@$_REQUEST['datewise']=="DATEWISE")
   								{
   								 echo '<td>' . $datetimeprocess. '</td>';	
   								}
							if(@$_REQUEST['hourwise']=="HOURWISE")
							 {
							 	 echo '<td>' . $Hour. '</td>';
							 }
							 echo '<td>'.$urlcount.'</td>';                 
	                        echo "</tr>";
	                    }  
	                 }else
					 {
					 echo "<tr><td colspan='6'>Record Not Found</td></tr>";	
					 }
					
					?>                       
                    </table>
                    <?php
				}else { echo '<p align="center"></br></br></br><b>You have not permission to access.</b></br></br></br></br></br></br></br></br></br></br></br></br></p>';}	?>
				
				            
                </div>
            </div>
        </div><!-- end row -->
    </div><!-- end container -->
</section>
<!-- end recent properties -->
<div class="bottomBar">
    <div class="container">
        <p>&nbsp;</p>
    </div>
</div>
<!-- JavaScript file links -->
<script src="js/jquery.js"></script>-->			<!-- Jquery -->
<script src="js/bootstrap.min.js"></script>  <!-- bootstrap 3.0 -->
<script src="js/respond.js"></script>
 <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<!-- Jquery datepicker End-->
 <script>
$(function() {
$( "#datepickerstart" ).datepicker();
});
$(function() {
$( "#datepickerend" ).datepicker();
});
</script>
<script language="javascript">
//frm is the form element
function checkForm(theForm){
 if (
	theForm.msisdndisplay.checked == false &&
	theForm.useragentdisplay.checked == false &&
	theForm.osdisplay.checked == false &&
	theForm.shorturldisplay.checked == false &&
	theForm.longurldisplay.checked == false) 
	{
		alert ('Please select atleast one displaying field!');
		return false;
	} else { 	
		return true;
	}
}

</script>
<!--	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>-->
		<script type="text/javascript">
	 $(document).ready(function() {
			var options = {
				chart: {
	                renderTo: 'container',
	                plotBackgroundColor: null,
	                plotBorderWidth: null,
	                plotShadow: false
	            },
	            title: {
	                text: 'Report Chart'
	            },
	            tooltip: {
	                formatter: function() {
	              //      return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
	                return '<b>'+ this.point.name +'</b>: '+ this.point.y  +' ';
	                }
	            },
	            plotOptions: {
	                pie: {
	                    allowPointSelect: true,
	                    cursor: 'pointer',
	                    dataLabels: {
	                        enabled: true,
	                        color: '#000000',
	                        connectorColor: '#000000',
	                        formatter: function() {
//	                            return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
	                            return '<b>'+ this.point.name +'</b>: '+ this.point.y +' ';

	                        }
	                    }
	                }
	            },
	            series: [{
	                type: 'pie',
	                name: 'Browser share',
	                data: []
	            }]
	        }
	        
	        $.getJSON("data.php?os=<?php echo @$_REQUEST['os'];?>&long_url=<?php echo @$_REQUEST['long_url'];?>&shorturl_code=<?php echo @$_REQUEST['shorturl_code'];?>&handset=<?php echo @$_REQUEST['handset'];?>&datepickerstart=<?php echo @$_REQUEST['datepickerstart'];?>&datepickerend=<?php echo @$_REQUEST['datepickerend'];?>&msisdndisplay=<?php echo @$_REQUEST['msisdndisplay'];?>&useragentdisplay=<?php echo @$_REQUEST['useragentdisplay'];?>&osdisplay=<?php echo @$_REQUEST['osdisplay'];?>&shorturldisplay=<?php echo @$_REQUEST['shorturldisplay'];?>&longurldisplay=<?php echo @$_REQUEST['longurldisplay'];?>", function(json) {
			options.series[0].data = json;
	        	chart = new Highcharts.Chart(options);
	        });
      	});    
		</script>
<!--		<script src="http://code.highcharts.com/highcharts.js"></script>
        <script src="http://code.highcharts.com/modules/exporting.js"></script>-->
        <script src="<?php echo ROOTURL;?>js/highcharts.js"></script>
        <script src="<?php echo ROOTURL;?>js/modules/exporting.js"></script>


</body>
</html>